﻿#pragma once


#ifdef WIN32
#include <queue>
#include "Misc/CommonMacros.h"
#include "PlatformInterface.h"

class ISytemWindowInterface;
class CTextInputInterface;
class CWindowsWindow;
class CWindowsOSThread;


class CWindowsPlatform : public IPlatformInterface
{
public:
    CWindowsPlatform() ;
    virtual ~CWindowsPlatform() override;

    static CWindowsPlatform* Get();
    
    virtual EPlatform GetPlatform() const override
    {
        return EPlatform::Windows;
    }

    virtual EArchitecture GetArchitecture() const override
    {
        return EArchitecture::x64;    
    }

    int64_t CreateProcess(const CString& Command, const CString& WorkingDirectory) override
    {
        //because CreateProcess is a windows macro, we need to rename it
        return CreateWindowsProcess(Command, WorkingDirectory);
    }
    
    //create a process and immediately return
    //return process id
    //return -1 if failed
    virtual int64_t CreateProcessWithoutBlock(const CString& Command, const CString& WorkingDirectory) override;

    virtual void KillProcess(int64_t ProcessID) override;

protected:
    virtual TVector<const char*> GetVulkanInstanceExtensions() const override;
    
    //return exit code
    int64_t CreateWindowsProcess(const CString& Command, const CString& WorkingDirectory);
        
    virtual  TOptional<SystemEvent> PopEvents() override;

    virtual ISytemWindowInterface * CreateSystemWindow() override;

    virtual void DestroySystemWindow(ISytemWindowInterface * Window) override;

    virtual SRect GetDisplayWorkArea(const SIntPoint & InPosition ) const  override;

    virtual void StartTextInput(uint64_t WindowID, SIntPoint IMEWidgetPosition) override;

    virtual void StopTextInput() override;

    virtual bool IsTextInputStarted() const override;

    virtual void UseSystemCursor(EMouseCursorType CursorType) override;

    virtual TVector<TTuple<CString /*Displayname*/,SPath>> GetDefaultFavoriteList() override;

    virtual bool IsKeyDown(EKey Key) override;

    virtual void SetClipboardText(const CString& Text) override;
    virtual bool HasClipboardText() override;
    virtual CString GetClipboardText() override;

    //捕获鼠标，即便鼠标移出窗口，也能接收到鼠标事件
    virtual void CaptureMouse(uint64_t WindowID) override;
    virtual void ReleaseMouseCapture() override;

public:
    //convert a commandline string to unicode
    //on windows, commandline string codec is ANSI,ANSI is changed by system locale
    virtual CString SystemStringToUnicode(const char * InArg) const override;

    std::queue<SystemEvent> EventQueue;

    
    

    CWindowsOSThread* WindowThread=nullptr;
};

#endif